package com.fastloan.mgt.product.postloan.service.impl;

import com.fastloan.common.BasicServiceImpl;
import com.fastloan.mgt.dictionary.service.DictionaryService;
import com.fastloan.mgt.product.basics.model.Product;
import com.fastloan.mgt.product.basics.service.ProductService;
import com.fastloan.mgt.product.postloan.dao.ProductPostLoanMapper;
import com.fastloan.mgt.product.postloan.model.ProductPostLoan;
import com.fastloan.mgt.product.postloan.model.vo.ProductPostLoanVO;
import com.fastloan.mgt.product.postloan.service.ProductPostLoanService;
import com.fastloan.utils.DozerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Optional;

import static com.fastloan.mgt.product.enums.ProductEnum.PUBLISHED;
import static com.fastloan.mgt.product.enums.ProductEnum.UNPUBLISHED;

/**
 * @author admins
 *         createTime 2017/11/10
 */
@Service
public class ProductPostLoanServiceImpl extends BasicServiceImpl<ProductPostLoan> implements ProductPostLoanService {
    @Autowired
    private ProductPostLoanMapper productPostLoanMapper;

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private ProductService productService;

    @Override
    public ProductPostLoanVO queryProductPostLoan(String productId) {
        ProductPostLoan productPostLoan = new ProductPostLoan();
        productPostLoan.setProductId(productId);
        productPostLoan.setLoanStatus(UNPUBLISHED.getType());
        Optional<ProductPostLoan> loan = select(productPostLoan).stream().findAny();
        ProductPostLoanVO postLoanVO = new ProductPostLoanVO();
        if (loan.isPresent()) {
            ProductPostLoan postLoan = loan.get();
            postLoanVO = DozerUtil.map(postLoan, ProductPostLoanVO.class);
        }
        postLoanVO.setDictionary(dictionaryService.queryDictionary("postloan"));
        return postLoanVO;
    }

    @Override
    @Transactional
    public Integer pagePostLoan(ProductPostLoan productPostLoan) {
        String productId = productPostLoan.getProductId();
        Product product = productService.selectByPrimaryKey(productId);
        ProductPostLoan postLoan = new ProductPostLoan();
        postLoan.setProductId(productId);
        postLoan.setLoanStatus(UNPUBLISHED.getType());
        if (!product.getStatus().equals(UNPUBLISHED.getType())) {
            List<ProductPostLoan> postLoanList = select(postLoan);
            postLoanList.forEach(productPostLoan1 -> {
                productPostLoan1.setLoanStatus(PUBLISHED.getType());
                updateByPrimaryKeySelective(productPostLoan1);
            });
        }
        delete(postLoan);
        return insertSelective(productPostLoan);
    }

    @Override
    public Integer postLoanCount(String productId) {
        Example example = new Example(ProductPostLoan.class);
        example.createCriteria().andEqualTo("productId", productId)
                .andEqualTo("loanStatus", UNPUBLISHED.getType());
        return selectCountByExample(example);
    }







}
